package com.sjq.common.web;

import java.util.Map;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sjq.common.entity.Dept;
import com.sjq.common.entity.Result;
import com.sjq.common.service.DeptService;
import com.sjq.common.util.PageResults;

@Controller
public class DeptController {
	
	 private static final Logger LOG  = LoggerFactory.getLogger(DeptController.class);
	@Autowired
    private DeptService deptService;
	private static final String INITURL = "/sys-manage/dept-manage";
    
	//初始化
    @RequestMapping(INITURL)
    public ModelAndView init(Integer pageNo, Integer pageSize, Dept dept){
    	if(dept==null){
    		dept=new Dept();
    	}
    	pageNo = pageNo == null ? 1 : pageNo;
        PageResults<?> pageResults=deptService.findDeptByPage(pageNo, PageResults.ALLPAGESIZE, dept);
    	ModelAndView mav = new ModelAndView();
        mav.setViewName("/view/user/deptList");
        mav.addObject("allDept", pageResults.getResults());
        mav.addObject("pageNo", pageResults.getPageNo());
        mav.addObject("pages", pageResults.getPageCount());
        mav.addObject("pageSize", pageResults.getPageSize());
        return mav;
    }
    
    //跳转新增或修改
    @RequestMapping(INITURL+"/deptModify")
    public  ModelAndView showNoteModify(@RequestParam("deptId") String deptId) throws Exception{

    	if(deptId.equals("-1")){
    		return new ModelAndView("/view/user/deptModify");
    	}else{
    		Map<String, Object> dept=deptService.findDeptById(deptId);
    		ModelAndView mav=new ModelAndView();
			mav.setViewName("/view/user/deptModify");
			mav.addObject("dept",dept);
			return mav;
    	}
    }
   
    //跳转详情
    @RequestMapping(INITURL+"/deptDetails")
    public ModelAndView showDeptDetails(String deptId){
    	Map<String, Object> dept=deptService.findDeptById(deptId);
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/view/user/deptDetails");
		mav.addObject("dept",dept);
		return mav;
    }
    
    
    //新增单位
    @RequestMapping("/deptAdd")
    public  ModelAndView addDeptInfo(@RequestParam("dwbm") String dwbm,@RequestParam("jc") String jc,@RequestParam("dwmc") String dwmc,@RequestParam("sjbmdm") String sjbmdm) throws Exception{
    	Dept dept = new Dept();
    	dept.setDWBM(dwbm);
    	dept.setDWMC(dwmc);
    	dept.setJC(jc);
    	dept.setSJBMDM(sjbmdm);
    	deptService.addDept(dept);
    	ModelAndView mav=new ModelAndView("view/user/deptModify");
    	return mav;
    }
  //新增单位时查看单位是否已存在
    @RequestMapping("/checkDwbmExist")
    @ResponseBody
    public  Result checkDwbm(@RequestParam("dwbm") String dwbm) throws Exception{
    	Map<String,Object> map=deptService.findDeptById(dwbm);
    	Result rs=new Result();
    	if(map!=null){
    		rs.setMsg("单位编码已存在,请重新输入单位编码!");
    		rs.setStatus(0);
    		rs.setData(map);
    	}else{
    		rs.setStatus(1);	
    	}
    	return rs; 
    }
    //修改单位
    @RequestMapping("/deptModify")
    public ModelAndView modifyDeptInfo(@RequestParam("dwbm") String dwbm,@RequestParam("jc") String jc,@RequestParam("dwmc") String dwmc,@RequestParam("sjbmdm") String sjbmdm) throws Exception{
    	Dept dept = new Dept();
    	dept.setDWBM(dwbm);
    	dept.setDWMC(dwmc);
    	dept.setJC(jc);
    	dept.setSJBMDM(sjbmdm);
    	deptService.modifyDept(dept);
    	ModelAndView mav=new ModelAndView("view/user/deptModify");
    	return mav;
    	
    }
    //删除单位
    @RequestMapping(INITURL+"/deptDelete")
    public ModelAndView deleteDeptInfo(@RequestParam("deptId") String dwbm) throws Exception{    	
    	deptService.removeDept(dwbm);
    	ModelAndView mav=new ModelAndView();
    	mav.setViewName("/view/user/deptList");
    	return mav;
    }
    
    //实例化单位列表
	@RequestMapping(INITURL+"/initDeptTree")
	public JSONObject initDeptTree(){
		
		
		return null;
	}
    
    
    
    
}
